<template>
  <view v-if="imgHost" :style="theme">
    <Homepage
      v-show="tabIndex == '/pages/index/index'"
      ref="homepageRef"
      :venueInfo="venueInfo"
      @bacBtn="bacBtn"
    />
    <IndexShop
      v-show="tabIndex == '/pages/index/indexShop'"
      ref="indexShopRef"
      @bacBtn="bacBtn"
    />
    <Homes v-show="tabIndex == '/pages/home/index'" ref="homeRef" />
    <EntranceAndExit
      v-show="tabIndex == '/pages/venue/entranceAndExit'"
      ref="exitRef"
    />
    <Curriculum
      v-show="tabIndex == '/pages/curriculum/index'"
      ref="curriculumRef"
    />
    <Activity v-show="tabIndex == '/pages/activity/index'" ref="activityRef" />
    <view style="height: 120rpx"></view>
    <Tabber :value="tabIndex" @bacBtn="bacBtn" />
  </view>
</template>

<script>
import Tabber from "@/components/tabber.vue";
import Homepage from "@/components/tab/index.vue";
import IndexShop from "@/components/tab/indexShop.vue";
import Homes from "@/components/tab/home.vue";
import Activity from "@/components/tab/activity.vue";
import EntranceAndExit from "@/components/tab/exit.vue";
import Curriculum from "@/components/tab/curriculum.vue";
// #ifdef H5
import wx from "@/common/jweixin.js";
// #endif
export default {
  components: {
    Tabber,
    IndexShop,
    Homepage,
    Activity,
    EntranceAndExit,
    Curriculum,
    Homes,
  },
  data() {
    return {
      tabIndex: "",
      loding: false,
      venueInfo: {},
      teacherId: "",
      venueList: [],
    };
  },
  async onLoad(ops) {
    await this.$onLaunched;
    this.loding = true;
    if (this.shopHome == 1) {
      this.$store.commit("setVenueId", "");
      this.tabIndex = ops.url || "/pages/index/indexShop";
      this.$refs.indexShopRef.showTab("start");
    } else {
      this.tabIndex = ops.url || "/pages/index/index";
      this.getCG();
    }
    uni.showShareMenu({
      withShareTicket: true,
      menus: ["shareAppMessage", "shareTimeline"],
    });
    this.teacherId = ops.teacherId;
    const scene = decodeURIComponent(ops.scene);
    if (scene && scene != "undefined") {
      uni.setStorage({
        key: "shareMemberId",
        data: scene,
      });
      if (this.userInfo) {
        this.addShareMemberRelevance(scene);
      }
    }
    // #ifdef H5
    this.getCofig();
    // #endif
  },
  async onShow() {
    await this.$onLaunched;
    if (this.shopHome == 1) {
      this.$store.commit("setVenueId", "");
    }
    if (this.tabIndex === "/pages/index/indexShop") {
      this.$refs.indexShopRef.showTab(1);
    } else if (this.tabIndex === "/pages/index/index") {
      this.$refs.homepageRef.showTab(1);
    } else if (this.tabIndex === "/pages/home/index") {
      this.$refs.homeRef.showTab();
    } else if (this.tabIndex === "/pages/activity/index") {
      this.$refs.activityRef.searchBtn();
    } else if (this.tabIndex === "/pages/curriculum/index") {
      if (this.teacherId) {
        this.$refs.curriculumRef.tabclick({
          index: 1,
          onTeach: [this.teacherId],
        });
      } else {
        let activeIndex = uni.getStorageSync("curriculumActive") || 0;
        this.$refs.curriculumRef.tabclick({
          index: activeIndex,
        });
      }
    } else if (this.tabIndex === "/pages/venue/entranceAndExit") {
      if (this.shopHome == 1) {
        this.$refs.exitRef.getCG();
      } else {
        this.$refs.exitRef.getList();
      }
    }
    let venueList = [...this.venueList];
    if (venueList.length > 0 && this.venueId) {
      venueList.forEach((s) => {
        if (this.venueId === s.id) {
          this.venueInfo = s;
          this.$store.commit("setVenueName", s.name);
          this.$store.commit("setVenueId", s.id);
          this.$store.commit("setVenuephone", s.telephone);
          this.$store.commit("setAlbumStatus", s.albumStatus);
        }
      });
    }
  },
  methods: {
    bacBtn(e) {
      this.tabIndex = e;
      if (this.tabIndex === "/pages/index/indexShop") {
        this.$refs.indexShopRef.showTab();
      } else if (e === "/pages/index/index") {
        this.$refs.homepageRef.showTab();
      } else if (this.tabIndex === "/pages/home/index") {
        this.$refs.homeRef.showTab();
      } else if (this.tabIndex === "/pages/activity/index") {
        this.$refs.activityRef.searchBtn();
      } else if (this.tabIndex === "/pages/curriculum/index") {
        this.$refs.curriculumRef.tabclick({
          index: 0,
        });
      } else if (this.tabIndex === "/pages/venue/entranceAndExit") {
        if (this.shopHome == 1) {
          this.$refs.exitRef.getCG();
        } else {
          this.$refs.exitRef.getList();
        }
      }
    },
    getCofig() {
      var url = location.href.split("#")[0];
      this.$app
        .ajax({
          data: {
            param: {
              path: url,
            },
          },
          api: this.$api.wxpConfig(),
        })
        .then((res) => {
          if (res.errorCode === "000000") {
            uni.setStorageSync("signature", JSON.stringify(res.record));
            wx.config({
              appId: res.record.appId, // 必填，企业号的唯一标识
              timestamp: res.record.timestamp, // 必填，生成签名的时间戳
              nonceStr: res.record.nonceStr, // 必填，生成签名的随机串
              signature: res.record.signature, // 必填，签名
              jsApiList: [
                "scanQRCode",
                "checkJsApi",
                "chooseImage",
                "updateAppMessageShareData",
                "updateTimelineShareData",
              ], // 必填，需使用的JS接口列表
            });
            wx.ready(() => {
              console.log("配置完成，扫码前准备完成");
            });
            wx.error(function (res) {
              //wx.config配置错误，会弹出窗口哪里错误，然后根据微信文档查询即可。
              console.log("出错了：" + res.errMsg);
            });
          }
        })
        .catch(() => {});
    },
    // 获取场馆 更新场馆id以及场馆名称
    getCG() {
      this.$app
        .ajax({
          data: {
            param: {
              lat: "",
              lng: "",
              openStatus: "1",
            },
          },
          api: this.$api.getVenue(),
        })
        .then((res) => {
          if (res.errorCode === "000000") {
            let arr = res.recordList;
            let index = 0;
            if (this.venueId) {
              arr.forEach((e, dex) => {
                if (e.id === this.venueId) {
                  index = dex;
                }
              });
            }
            this.venueList = res.recordList || [];
            this.venueInfo = res.recordList[index];
            this.$store.commit("setVenueName", res.recordList[index].name);
            this.$store.commit("setVenueId", res.recordList[index].id);
            this.$store.commit(
              "setVenuephone",
              res.recordList[index].telephone
            );
            this.$store.commit(
              "setAlbumStatus",
              res.recordList[index].albumStatus
            );
            this.$refs.homepageRef.showTab("start");
          }
        })
        .catch(() => {});
    },
    addShareMemberRelevance(shareMemberId) {
      this.$app
        .ajax({
          data: {
            param: {
              shareMemberId,
            },
          },
          api: this.$api.addShareMemberRelevance(),
        })
        .then((res) => {
          if (res.errorCode === "000000") {
            uni.removeStorageSync("shareMemberId");
          }
        });
    },
  },
  onReachBottom() {
    if (this.tabIndex !== "/pages/home/index") {
      uni.$emit("onReachBottom");
    }
  },
  onPageScroll(res) {
    if (this.tabIndex !== "/pages/home/index") {
      uni.$emit("onPageScroll", res.scrollTop);
    }
  },
  onShareAppMessage(e) {
    let path = this.tabIndex;
    if (e.from == "button") {
      let a = e.target.dataset.data;
      let name = a.name;
      let venueName = this.venueName;
      let time = a.useTime || a.expirationTime + "过期";
      let stra = a.itemName || "";
      let strb = a.areaName || "";
      let strc = a.siteModelName || "";
      let xm = stra + " | " + strb + " | " + strc;
      let title = this.userInfo.nickName + "分享给您的进出场凭证";
      let imageUrl = this.imgHost + "/img_share.png";
      return {
        title: title,
        path: `/pages/venue/receive?id=${a.code}&name=${name}&time=${time}&venueName=${venueName}&xm=${xm}`,
        imageUrl: imageUrl,
      };
    } else {
      return {
        path: "/pages/index/index?url=" + path, // 路径，传递参数到指定页面。
      };
    }
  },
};
</script>